home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / readers / skim-0.8 / skim-0 / skim-0.8.4 / GetSelectedArticlesInGroup.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  7KB  |  239 lines

  1. /*
  2.  * NAME
  3.  *   GetSelectedArticlesInGroup.c
  4.  * USAGE
  5.  *   Usage: GetSelectedArticlesInGroup Group
  6.  * DESCRIPTION
  7.  *   Retrieve the selected articles from the news server. 
  8.  * OUTPUT
  9.  *   Every article is stored in a separate file in the directory
  10.  *   $SKIMDIR/Articles. The file name is GroupName.ArticleNumber.
  11.  * COPYRIGHT
  12.  *   Skim - Off-line news reading package optimized for slow lines.
  13.  *   Copyright (C) 1996  Rene W.J. Pijlman
  14.  *
  15.  *   This program is free software; you can redistribute it and/or modify
  16.  *   it under the terms of the GNU General Public License as published by
  17.  *   the Free Software Foundation; either version 2 of the License, or
  18.  *   (at your option) any later version.
  19.  * 
  20.  *   This program is distributed in the hope that it will be useful,
  21.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *   GNU General Public License for more details.
  24.  * 
  25.  *   You should have received a copy of the GNU General Public License
  26.  *   along with this program; if not, write to the Free Software
  27.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  * VERSION
  29.  *   Skim version 0.8.4.
  30.  */
  31.  
  32. #include <unistd.h>
  33. #include <string.h>
  34.  
  35. #include "VarBuf.h"
  36. #include "Skim.h"
  37. #include "SkimUtils.h"
  38. #include "NNTPStream.h"
  39. #include "StandardIO.h"
  40.  
  41. FILE_ID("$Header: /home/rene/sys/CVS_MasterSourceRepository/skim/GetSelectedArticlesInGroup.c,v 1.6 1996/02/18 11:40:18 rene Exp $");
  42.  
  43.  
  44. int main( int argc, char * argv[] )
  45. {
  46.     VarBuf SubjectsFileName = VBCreate();
  47.     StandardIO SubjectsFile = SIOCreate();
  48.  
  49.     if ( argc != 2 )
  50.     {
  51.         SIOPrintf( StandardError, "%s\n",
  52.                "Usage: GetSelectedArticlesInGroup Group" );
  53.         exit( EXIT_FAILURE );
  54.     }
  55.  
  56.     if ( strlen(argv[1]) > 0 )
  57.     {
  58.     VBPrintf( SubjectsFileName, "%s/Subjects/%s", SkimDirectory(), 
  59.               argv[1] );
  60.  
  61.     SIOFileOpenVB( SubjectsFile, SubjectsFileName, OpenModeReadIgnore );
  62.  
  63.     if ( SIOIsOpenForRead(SubjectsFile) )
  64.     {
  65.         VarBuf SubjectLine = VBCreate();
  66.         VarBuf ArticleFileName = VBCreate();
  67.         VarBuf IndexFileName = VBCreate();
  68.         VarBuf SkippedFileName = VBCreate();
  69.         VarBuf Selection = VBCreate();
  70.         VarBuf ArticleNumber = VBCreate();
  71.         StandardIO SkippedFile = NULL;
  72.         StandardIO Index = NULL;
  73.         StandardIO NewsServer = NULL;
  74.         StandardIO ArticleFile = SIOCreate();
  75.  
  76.         VBPrintf( IndexFileName, "%s/Indexes/Articles/%s", SkimDirectory(), 
  77.               argv[1] );
  78.  
  79.         VBPrintf( SkippedFileName, "%s/Skipped/%s", SkimDirectory(), 
  80.                   argv[1] );
  81.  
  82.         while ( VBReadLine( SubjectLine, SubjectsFile, WITHOUT_NEWLINE ) )
  83.         {
  84.         if ( !AppendFieldFromFixedRecord( Selection, SubjectLine,
  85.                           OffsetOfSelection(), 
  86.                           LengthSelection,
  87.                           NULL ) )
  88.         {
  89.             SIOPrintf( StandardError,
  90.                  "Incorrect format: no 'Selection' field: %s\n",
  91.                  VBAsString(SubjectLine));
  92.             exit( EXIT_FAILURE );
  93.         }
  94.  
  95.         if ( *VBAsString(Selection) == Selected )
  96.         {
  97.             Boolean ArticleIsAvailable;
  98.             VarBuf Command = VBCreate();
  99.             VarBuf ArticleSkipped = VBCreate();
  100.             VarBuf SubjectOfArticle = VBCreate();
  101.  
  102.             if ( NewsServer == NULL )
  103.             {
  104.             NewsServer = NNTPStreamOpen();
  105.             }
  106.  
  107.             VBReset( Command );
  108.             VBPrintf( Command, "group %s", argv[1] );
  109.  
  110.             /* Make group current. */
  111.             SIOInternetCommand( NewsServer, VBAsString( Command ) );
  112.             CheckStatusResponse( NewsServer, argv[1], "2", NULL,
  113.                      TERMINATE_ON_ERROR );
  114.  
  115.             if (!AppendFieldFromFixedRecord( ArticleNumber, SubjectLine,
  116.                              OffsetOfArticleNumber(), 
  117.                              LengthArticleNumber, 
  118.                              VBRemoveTrailingBlanks ) )
  119.             {
  120.             SIOPrintf( StandardError, 
  121.                  "Incorrect format: No article number: %s\n",
  122.                  VBAsString(SubjectLine));
  123.             exit( EXIT_FAILURE );
  124.             }
  125.  
  126.             VBPrintf( ArticleFileName, "%s/Articles/%s.%V",
  127.                   SkimDirectory(), argv[1], ArticleNumber );
  128.  
  129.             /* Make group current. */
  130.             VBReset( Command );
  131.             VBPrintf( Command, "article %V", ArticleNumber );
  132.             SIOInternetCommand( NewsServer, VBAsString(Command) );
  133.  
  134.             /*
  135.              * Prepare a message which may be printed by 
  136.              * CheckStatusResponse().
  137.              */
  138.             if (!AppendFieldFromFixedRecord( SubjectOfArticle, SubjectLine,
  139.                              OffsetOfSubject(),
  140.                              VARIABLE_LENGTH,
  141.                              VBRemoveTrailingBlanks) )
  142.             {
  143.             SIOPrintf( StandardError,
  144.                  "Incorrect format: No subject: %s\n",
  145.                  VBAsString(SubjectLine));
  146.             exit( EXIT_FAILURE );
  147.             }
  148.             VBPrintf( ArticleSkipped,
  149.               "Warning: skipped article %V in newsgroup %s\nSubject: %V.\n\n",
  150.                   ArticleNumber, argv[1], SubjectOfArticle );
  151.  
  152.             /* Check status response. */
  153.             ArticleIsAvailable =
  154.             CheckStatusResponse( NewsServer, argv[1], "2",
  155.                          VBAsString(ArticleSkipped),
  156.                          DONT_TERMINATE_ON_ERROR );
  157.             if ( ArticleIsAvailable )
  158.             {
  159.             SIOFileOpenVB( ArticleFile, ArticleFileName,
  160.                        OpenModeWriteDiscardOld );
  161.  
  162.             /* Write the article in a file. */
  163.             GetTextResponse( NewsServer, NULL, NULL, ArticleFile, 
  164.                      NULL );
  165.  
  166.             SIOFileClose( ArticleFile );
  167.  
  168.             if ( Index == NULL )
  169.             {
  170.                 Index = SIOCreate();
  171.                 SIOFileOpenVB( Index, IndexFileName, OpenModeAppend );
  172.             }
  173.  
  174.             AddArticleToIndex( ArticleFileName, ArticleNumber, Index );
  175.             }
  176.  
  177.             VBDestroy(Command);
  178.             VBDestroy( SubjectOfArticle );
  179.             VBDestroy( ArticleSkipped );
  180.         }
  181.         else if ( *VBAsString(Selection) == NotSelected )
  182.         {
  183.             if ( SkippedFile == NULL )
  184.             {
  185.             SkippedFile = SIOCreate();
  186.  
  187.             SIOFileOpenVB( SkippedFile, SkippedFileName, 
  188.                        OpenModeAppend );
  189.             }
  190.  
  191.             SIOPrintf( SkippedFile, "%V\n", SubjectLine);
  192.         }
  193.         else
  194.         {
  195.             SIOPrintf( StandardError, 
  196.               "Incorrect format: not %c or %c on first position: %V\n", 
  197.               Selected, NotSelected, SubjectLine );
  198.             exit( EXIT_FAILURE );
  199.         }
  200.  
  201.         VBReset( ArticleNumber );
  202.         VBReset( ArticleFileName );
  203.         VBReset( SubjectLine );
  204.         VBReset( Selection );
  205.         }
  206.  
  207.         NNTPStreamClose( NewsServer );
  208.         NewsServer = NULL;
  209.  
  210.         SIODestroy( Index );
  211.  
  212.         SIODestroy(SkippedFile);
  213.  
  214.         SIOFileClose( SubjectsFile );
  215.  
  216.         if ( remove( VBAsString(SubjectsFileName) ) != 0 )
  217.         {
  218.         SIOPrintf( StandardError, "Error removing %V\n", 
  219.                    SubjectsFileName );
  220.         exit( EXIT_FAILURE );
  221.         }
  222.  
  223.         VBDestroy(SubjectLine);
  224.         VBDestroy(ArticleFileName);
  225.         VBDestroy(IndexFileName);
  226.         VBDestroy(SkippedFileName);
  227.         VBDestroy(Selection);
  228.         VBDestroy(ArticleNumber);
  229.  
  230.         SIODestroy(ArticleFile);
  231.     }
  232.     }
  233.  
  234.     VBDestroy(SubjectsFileName);
  235.     SIODestroy(SubjectsFile);
  236.  
  237.     return EXIT_SUCCESS;
  238. }
  239.